import { http } from '@/utils/request';

const getAuthorization = () => {
  return sessionStorage.getItem('token') || '';
};

const getSearchParams = (params) => {
  const arr = [];
  for (const key in params) {
    if (key != 'YTO-TGC') {
      arr.push(`${key}=${params[key]}`);
    }
  }
  return arr.join('&');
};

/**
 * 账号密码登录
 * @returns UseAxiosReturn
 */
export function loginApi(data) {
  return http.post(`/api/login`, data);
}

/**
 * 退出登录
 * @returns UseAxiosReturn
 */
export function logoutApi(data) {
  return http.post(`/api/logout`, data);
}

/**
 * 账号密码注册
 * @returns UseAxiosReturn
 */
export function registerApi(data) {
  return http.post(`/api/register`, data);
}

/**
 * 获取图形验证码
 * @returns UseAxiosReturn
 */
export function captchaApi(params) {
  return http.get(`/api/captcha`, {
    params,
  });
}

/**
 * 发送邮件验证码
 * @returns UseAxiosReturn
 */
export function sendMailCodeApi(data) {
  return http.post(`/api/send-mail-code`, data);
}

/**
 * 修改密码
 * @returns UseAxiosReturn
 */
export function changePassApi(data) {
  return http.post(`/api/user/change-pass`, data);
}

/**
 * 获取用户信息
 * @returns UseAxiosReturn
 */
export function userApi(params) {
  return http.get(`/api/user`, {
    params,
  });
}

/**
 * 商品列表
 * @returns UseAxiosReturn
 */
export function goodsApi(params) {
  return http.get(`/api/goods`, {
    params,
  });
}

/**
 * 生成代理
 * @returns UseAxiosReturn
 */
export function proxyCreateApi(data) {
  return http.post(`/api/proxy/dynamic`, data);
}

/**
 * 代理记录
 * @returns UseAxiosReturn
 */
export function proxyListApi(params) {
  return http.get(`/api/proxy/dynamic`, {
    params,
  });
}

/**
 * 下载代理
 * @returns UseAxiosReturn
 */
export function proxyDownloadApi(params) {
  return http.get(`/api/proxy/dynamic/export`, {
    params,
    headers: {
      responseType: 'blob',
    },
  });
}

/**
 * 删除代理
 * @returns UseAxiosReturn
 */
export function proxyDeleteApi(params) {
  return http.delete(`/api/proxy/dynamic`, {
    params,
  });
}

/**
 * 获取国家/地区，州/省，区/县
 * @returns UseAxiosReturn
 */
export function regionApi(params) {
  return http.get(`/api/region`, {
    params,
  });
}

/**
 * 订单记录
 * @returns UseAxiosReturn
 */
export function orderApi(params) {
  return http.get(`/api/record/dynamic-order`, {
    params,
  });
}

/**
 * 流量使用记录
 * @returns UseAxiosReturn
 */
export function trafficApi(params) {
  return http.get(`/api/traffic`, {
    params,
  });
}

/**
 * 我的分销
 * @returns UseAxiosReturn
 */
export function distributionInfoApi(params) {
  return http.get(`/api/distribution/info`, {
    params,
  });
}

/**
 * 我的邀请
 * @returns UseAxiosReturn
 */
export function distributionInviteApi(params) {
  return http.get(`/api/distribution/invite`, {
    params,
  });
}

/**
 * 我的结算
 * @returns UseAxiosReturn
 */
export function distributionPayoffApi(params) {
  return http.get(`/api/distribution/payoff`, {
    params,
  });
}

/**
 * 分销用户列表
 * @returns UseAxiosReturn
 */
export function distributionUserApi(params) {
  return http.get(`/api/distribution/user`, {
    params,
  });
}

/**
 * 用户列表
 * @returns UseAxiosReturn
 */
export function manageUserApi(params) {
  return http.get(`/api/manage/user`, {
    params,
  });
}

/**
 * 充值记录
 * @returns UseAxiosReturn
 */
export function manageOrderApi(params) {
  return http.get(`/api/manage/order`, {
    params,
  });
}

/**
 * 用户列表
 * @returns UseAxiosReturn
 */
export function manageUserExportApi(params) {
  // return http.get(`/api/manage/user`, {
  //   params,
  //   headers: {
  //     responseType: 'blob',
  //   },
  // });
  const arr = [];
  for (const key in params) {
    if (key != 'YTO-TGC') {
      arr.push(`${key}=${params[key]}`);
    }
  }
  window.open(`${window.location.origin}/api/manage/user?${getSearchParams(params)}&token=${getAuthorization()}`, '_blank');
}

/**
 * 充值记录-导出
 * @returns UseAxiosReturn
 */
export function manageOrderExportApi(params) {
  // return http.get(`/api/manage/order`, {
  //   params,
  //   headers: {
  //     responseType: 'blob',
  //   },
  // });
  window.open(`${window.location.origin}/api/manage/order?${getSearchParams(params)}&token=${getAuthorization()}`, '_blank');
}

/**
 * 支付配置
 * @returns UseAxiosReturn
 */
export function payConfigApi(params) {
  return http.get(`/api/manage/pay-config`, {
    params,
  });
}

/**
 * 支付配置
 * @returns UseAxiosReturn
 */
export function savePayConfigApi(data) {
  return http.post(`/api/manage/pay-config`, data);
}

/**
 * 创建订单
 * @returns UseAxiosReturn
 */
export function buyDynamicApi(data) {
  return http.post(`/api/buy/dynamic`, data);
}

/**
 * 创建订单
 * @returns UseAxiosReturn
 */
export function orderCheckApi(data) {
  return http.post(`/api/order-check`, data);
}

/**
 * 编辑分销用户
 * @returns UseAxiosReturn
 */
export function distributionSaveuserApi(data) {
  return http.post(`/api/distribution/saveuser`, data);
}

/**
 * 切换分销状态
 * @returns UseAxiosReturn
 */
export function distributionStatusApi(data) {
  return http.post(`/api/distribution/status`, data);
}

/**
 * 分销结算
 * @returns UseAxiosReturn
 */
export function distributionCreatePayoffApi(data) {
  return http.post(`/api/distribution/payoff`, data);
}

/**
 * 充值
 * @returns UseAxiosReturn
 */
export function manageRechargeApi(data) {
  return http.post(`/api/manage/recharge`, data);
}

/**
 * 重置密码
 * @returns UseAxiosReturn
 */
export function manageResetPassApi(data) {
  return http.post(`/api/manage/reset-pass`, data);
}

/**
 * 订单核对
 * @returns UseAxiosReturn
 */
export function manageOrderVerifyApi(data) {
  return http.post(`/api/manage/order-verify`, data);
}

/**
 * 渠道数据
 * @returns UseAxiosReturn
 */
export function distributionCannelApi(params) {
  return http.get(`/api/distribution/channel`, {
    params,
  });
}

/**
 * 立即购买点击日志
 * @returns UseAxiosReturn
 */
export function logApi(data) {
  return http.post(`/api/logs/intention-purchase`, data);
}

/**
 * 日志查看
 * @returns UseAxiosReturn
 */
 export function manageLogApi(params) {
  return http.get(`/api/manage/logs`, {
    params,
  });
}

/**
 * 日志查看-导出
 * @returns UseAxiosReturn
 */
 export function manageLogExportApi(params) {
  // return http.get(`/api/manage/log`, {
  //   params,
  //   headers: {
  //     responseType: 'blob',
  //   },
  // });
  window.open(`${window.location.origin}/api/manage/logs?${getSearchParams(params)}&token=${getAuthorization()}`, '_blank');
}

/**
 * 访问统计
 * @returns UseAxiosReturn
 */
 export function manageStatisticsApi(params) {
  return http.get(`/api/manage/statistics`, {
    params,
  });
}

/**
 * 访问统计-导出
 * @returns UseAxiosReturn
 */
 export function manageStatisticsExportApi(params) {
  // return http.get(`/api/manage/statistics`, {
  //   params,
  //   headers: {
  //     responseType: 'blob',
  //   },
  // });
  window.open(`${window.location.origin}/api/manage/statistics?${getSearchParams(params)}&token=${getAuthorization()}`, '_blank');
}

/**
 * 静态代理-订单记录
 * @returns UseAxiosReturn
 */
export function staticProxyListApi(params) {
  return http.get(`/api/proxy/static`, {
    params,
  });
}

/**
 * 静态代理-订单记录
 * @returns UseAxiosReturn
 */
export function staticOrderApi(params) {
  return http.get(`/api/record/static-order`, {
    params,
  });
}

/**
 * 静态代理-获取国家/地区，州/省，区/县
 * @returns UseAxiosReturn
 */
export function staticRegionApi(params) {
  return http.get(`/api/static-region`, {
    params,
  });
}

/**
 * 静态代理-业务用途
 * @returns UseAxiosReturn
 */
export function businessApi(params) {
  return http.get(`/api/business`, {
    params,
  });
}

/**
 * 创建静态订单
 * @returns UseAxiosReturn
 */
export function buyStaticApi(data) {
  return http.post(`/api/buy/static`, data);
}

/**
 * 静态订单续费
 * @returns UseAxiosReturn
 */
export function buyStaticRenewApi(data) {
  return http.post(`/api/buy/static-renew`, data);
}

/**
 * 静态充值
 * @returns UseAxiosReturn
 */
export function manageStaticRechargeApi(data) {
  return http.post(`/api/manage/static-recharge`, data);
}

/**
 * 静态续费
 * @returns UseAxiosReturn
 */
export function manageStaticRenewRechargeApi(data) {
  return http.post(`/api/manage/static-renew-recharge`, data);
}

